package com.zq.common.mail;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.Random;

/**
 * @Author 张乔
 * @Date 2024/5/8 18:07
 */
//@Service
////排除网关服务
//@ConditionalOnClass(DispatcherServlet.class)
public class SendMailService {

@Resource
    private JavaMailSenderImpl javaMailSender;

@Value("${spring.mail.username}")
private String from;



public void sendMail(String to, String subject, String content) {
    // 创建邮件对象
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from); // 设置发件人
    message.setTo(to); // 设置收件人
    message.setSubject(subject); // 设置主题
    message.setText(content); // 设置内容
    javaMailSender.send(message); // 发送邮件
}



public String sendCodeMail(String to) {
    // 创建 Random 对象
    Random random = new Random();
    // 生成随机的 6 位数字验证码
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 6; i++) {
        int digit = random.nextInt(10); // 生成 0 到 9 之间的随机整数
        sb.append(digit);
    }

    String verificationCode = sb.toString();

        // 构建邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); // 设置发件人
        message.setTo(to); // 设置收件人
        message.setSubject("重置密码"); // 设置主题
        message.setText(verificationCode+"\n5分钟有效"); // 设置内容
        javaMailSender.send(message); // 发送邮件
    return verificationCode;
}


}
